<template>
  <el-dialog :title="'从物料库中选择物料'" v-model="dialogVisible" width="1200px" append-to-body @open="open" :close-on-click-modal="false">
    <div class="material-table">
      <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="68px">
        <el-form-item label="名称" prop="name">
          <el-input v-model.trim="queryParams.name"  @input="handleInput" placeholder="请输入名称" clearable style="width: 340px"
             />
        </el-form-item>
        <el-form-item label="规格/型号" prop="specifications"  label-width="100px">
          <el-input v-model.trim="queryParams.specifications"  @input="handleInput" placeholder="请输入规格/型号" clearable style="width: 340px"
             />
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item> -->
      </el-form>
      <el-row :gutter="10" class="mb8">
        <!-- <right-toolbar :search="false"  :columns="columns"></right-toolbar> -->
      </el-row>
      <el-table v-loading="loading" size="large" :max-height="tableHeight" :data="dataList" @row-click="handleRowClick"
        row-key="id" style="width: 100%">
        <el-table-column width="100" align="center">
          <template #header>
            <span class="select-header">请选择物料</span>
          </template>
          <template #default="scope">
            <el-radio v-model="selectedId" :label="scope.row.id" @click.stop="() => handleRadioChange(scope.row)"
              class="custom-radio">&nbsp;</el-radio>
          </template>
        </el-table-column>

        <el-table-column label="名称" prop="name" align="center" min-width="200" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ scope.row.name || '' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规格" show-overflow-tooltip prop="specifications" align="center" min-width="150"
          v-if="columns[0].visible" />
        <el-table-column label="型号" show-overflow-tooltip prop="types" align="center" min-width="150"
          v-if="columns[1].visible" />
        <el-table-column label="制造厂商" show-overflow-tooltip prop="manufacturer" align="center" min-width="180"
          v-if="columns[2].visible" />
        <el-table-column label="颜色" show-overflow-tooltip prop="colors" align="center" min-width="100"
          v-if="columns[3].visible" />
        <el-table-column label="成本(元)" show-overflow-tooltip prop="price.cost" align="center" min-width="100"
          v-if="columns[4].visible">
          <template #default="scope">
            <!-- 对 price.cost 乘以 100 并保留两位小数 -->
            <span>{{ (scope.row.price?.cost / 100).toFixed(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单价(元)" show-overflow-tooltip prop="price.price1Show" align="center" min-width="100"
          v-if="columns[5].visible" />
      </el-table>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
const { proxy } = getCurrentInstance();

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  showSearch: {
    type: Boolean,
    default: true
  },
  queryParams: {
    type: Object,
    default: () => ({
      name: null,
      specifications: null,
    })
  }
  
});

// 列显隐信息
const columns = ref([
  { key: 0, label: `规格`, visible: true },
  { key: 1, label: `型号`, visible: false },
  { key: 2, label: `制造厂商`, visible: true },
  { key: 3, label: `颜色`, visible: false },
  { key: 4, label: `成本`, visible: true },
  { key: 5, label: `单价`, visible: true }
]);

const emit = defineEmits(['update:modelValue', 'select', 'query', 'cancel','onInput']);

const dialogVisible = computed({
  get: () => props.modelValue,
  set: (val) => emit('update:modelValue', val)
});

// 当前选中行
const currentRow = ref(null);

// 表格高度
const tableHeight = ref(document.documentElement.scrollHeight - 400 + "px");

const queryRef = ref(null);
const loading = ref(false);
const dataList = ref([]);

// 重置按钮操作
// 修改 setDataList 方法
defineExpose({
  setLoading: (val) => loading.value = val,
  setDataList: (val) => {
    dataList.value = val;
    // 如果有数据，自动选中第一条
    if (val && val.length > 0) {
      const firstRow = val[0];
      selectedId.value = firstRow.id;
      currentRow.value = firstRow;
    }else{
      selectedId.value = null;
      currentRow.value = null;
    }
  },
});
// 输入时触发的防抖处理函数
let timer = null;
function handleInput() {
  clearTimeout(timer);
  timer = setTimeout(handleQuery, 300);
}

// 修改取消按钮处理方法
function handleQuery() {
  emit('onInput');
}

// 修改重置方法，清空选中状态
function resetQuery() {
  queryRef.value?.resetFields();
  selectedId.value = '';
  currentRow.value = null;
}

// 行选中事件
function handleCurrentChange(row) {
  currentRow.value = row;
}

// 确定按钮
function handleConfirm() {
  if (!currentRow.value) {
    proxy.$modal.msgError("请选择物料");
    return;
  }
  emit('select', currentRow.value);
  dialogVisible.value = false;
}

// 取消按钮
const selectedId = ref('');

// 修改行点击处理方法
function handleRowClick(row) {
  selectedId.value = row.id;
  currentRow.value = row;
}

// 添加单选框变化处理方法
function handleRadioChange(row) {
  currentRow.value = row;
}

// 修改取消按钮处理方法
function handleCancel() {
  currentRow.value = null;
  selectedId.value = '';
  dialogVisible.value = false;
  emit('cancel');
}

// 暴露方法给父组件
function open() {
}
</script>
<style lang="scss" scoped>
.material-table {
  width: 100%;
}

.mb8 {
  margin-bottom: 8px;
}

:deep(.custom-radio) {
  margin-right: 0;

  .el-radio__inner {
    width: 20px;
    height: 20px;

    &::after {
      width: 8px;
      height: 8px;
    }
  }
}

.link-type {
  color: #409EFF;
  cursor: pointer;

  &:hover {
    color: #79bbff;
  }
}
.select-header {
  color: #409EFF;
  font-weight: bold;
}
</style>